import yRequest from "@/utils/request";
import {DefaultResult, LoginForm, MenuData, TokenRes, UserPrincipal} from "@/apis/types";
import request from "@/utils/request";
// @ts-ignore
import qs from "qs";

export enum LoginApi{
    getToken = "/auth/token",
    UserPrincipal="/auth/principle",
    MyMenu="/core/menu/my",
    refreshToken="/auth/refresh/"
}

/**
 * 账号密码登陆获得token
 *
 * @param loginForm
 * @constructor
 */
export const ACCOUNT_LOGIN = (loginForm: LoginForm) => {
    return request.post<DefaultResult<TokenRes>>({
        url: LoginApi.getToken,
        data: qs.stringify(loginForm),
        headers: {
            "Content-Type": "application/x-www-form-urlencoded"
        }
    })
}

/**
 * 刷新token
 *
 * @param refreshToken
 * @constructor
 */
export const REFRESH_TOKEN = (refreshToken:any) => {
    return request.post<DefaultResult<TokenRes>>({
        url: LoginApi.refreshToken + refreshToken
    })
}

/**
 * 获得登陆用户信息
 *
 * @constructor
 */
export const GET_USER_PRINCIPAL = () => {
    return request.get<DefaultResult<UserPrincipal>>({
        url: LoginApi.UserPrincipal,
        showLoading: false
    })
}

/**
 * 查询我的菜单
 *
 * @constructor
 */
export const QUERY_MY_MENU = () => {
    return request.get<DefaultResult<MenuData[] >>({
        url: LoginApi.MyMenu,
        showLoading: false
    })
}
